<?php
class model_newagk_examspapers extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'agk_exams_papers';
    }
	public function getOnes($id){

		if(empty($id)) return false;
		$result = $this->selectOne(['id'=>$id],'*');
		return $result;
		
	 }
	 
	 public function getInfoArgOne($condition,$field='*'){

		if(empty($condition)) return false;
		$result = $this->selectOne($condition,$field);
		return $result;
		
	 }

	public function repl($id){
		$types= ['single'=>'1','more'=>'2','judge'=>'3','gapFilling'=>'4','subjectivity'=>'5'];
		return empty($types[$id]) ? '1' : $types[$id];
	}
	public function replf($id){
		$types= ['1'=>'single','2'=>'more','3'=>'judge','4'=>'gapFilling','5'=>'subjectivity'];
		return empty($types[$id]) ? 'single' : $types[$id];
	}
	
	//app 交卷用户算分
	public function scores($sessionvars,$useranswer,$type=0){
		$es_question = unserialize($sessionvars['question']);
		
		
        $es_setting = unserialize($sessionvars['setting']);
		$dw_sorc = unserialize($es_setting['setting']);
		//$dw_sorc = unserialize('a:2:{i:0;a:5:{i:0;s:1:"1";i:1;s:1:"1";i:2;s:3:"148";i:3;i:3;i:4;i:4;}i:1;a:5:{i:0;s:1:"2";i:1;s:1:"1";i:2;s:3:"166";i:3;i:2;i:4;i:4;}}');
		$scores = [];
        foreach ($dw_sorc as $vs) {
             $scores[$vs['0']][$vs['2']] = [$vs['3'], $vs['4']];
        }
		
		$scores= [];
		$scores2 = [];
		 foreach ($dw_sorc as $vs) {
             $scores[$vs['0']][$vs['2']] = [$vs['3'], $vs['4']];
			 $scores2[$vs['0']] = [$vs['3'], $vs['4']];
        }
		
		
		

		$p_setting = $scores;
		$questionsList = [];
		foreach ($es_question as $qkey => $qval) {
                $questionsList[$this->repl($qkey)] = $qval;
				
        }
		$answer = [];
		foreach($useranswer as $v){

				$answer[$v['id']] = $v['answer'];

		}

		$score = 0; //用户的分
		$pass=0;
		$nopass=0;
		$num = [];
		$especially = 3;
		foreach ($questionsList as $key => $tmp) {
				$qkeys = $this->replf($key);

				$num[$key] = count($tmp); //每种类型多少题
                if ($key>3){ //$type==0 && 
                    $especially=2;
                    continue;
                }
				foreach($tmp as $k=>$v){
					if(empty($scores[$key][$v['sectionId']])){
						$es_question[$qkeys][$k]['minute'] = $scores2[$key][1];
					} else {
						$es_question[$qkeys][$k]['minute'] = $scores[$key][$v['sectionId']][1];	
					}
					$es_question[$qkeys][$k]['score']  = 0;

                    if(!empty($answer[$v['id']])){

                        if ($v['typeId']==4 || $v['typeId']==5){
                            if($answer[$v['id']][1]=='y'){
								if(empty($scores[$key][$v['sectionId']])){
									$score += $scores2[$key][1];
									$es_question[$qkeys][$k]['score'] = $scores2[$key][1];
								} else {
									$score += $scores[$key][$v['sectionId']][1];
									$es_question[$qkeys][$k]['score'] = $scores[$key][$v['sectionId']][1];
								}
                                $pass++;
                            } else if($answer[$v['id']][1]=='n'){
                                $nopass++;
                            }
                        }else{
                            if($answer[$v['id']] == $v['answer']){
								
								if(empty($scores[$key][$v['sectionId']])){
									$score += $scores2[$key][1];
									$es_question[$qkeys][$k]['score'] = $scores2[$key][1];
								} else {
									$score += $scores[$key][$v['sectionId']][1];
									$es_question[$qkeys][$k]['score'] = $scores[$key][$v['sectionId']][1];
								}
                                $pass++;
                            } else {
                                $nopass++;
                            }
                        }
                    } else {
                        $nopass++;
                    }
				}

        }
		return ['score'=>$score,'num'=>$num,'especially'=>$especially,'pass'=>$pass,'nopass'=>$nopass,'test'=>$es_setting,'question'=>$es_question];
	}
    
}
